package com.example.databasediary;


import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Manifest;


public class DiaryEditor extends Activity {

    private static final String TAG = "Diary";
    public static final String EDIT_DIARY_ACTION
            = "com.example.mydiary.DiaryEditor.EDIT_DIARY";
    public static final String INSERT_DIARY_ACTION
            = "com.example.mydiary.DiaryEditor.action.INSERT_DIARY";

    //插入图片
//    public static final int TAKE_PHOTO = 1;
//    private ImageView picture;
//    private Uri imageUri;


    /**
     * 查询cursor时候，感兴趣的那些条例。
     */
    private static final String[] PROJECTION
            = new String[] { Fields.DiaryColumns._ID, // 0
            Fields.DiaryColumns.TITLE, Fields.DiaryColumns.BODY, //
    };

    private static final int STATE_EDIT = 0;
    private static final int STATE_INSERT = 1;
    private int mState;
    private Uri mUri;
    private Cursor mCursor;
    private EditText mTitleText;
    private EditText mBodyText;
    private Button confirmButton;
    private Button button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(android.R.style.Theme_Black);
        final Intent intent = getIntent();
        final String action = intent.getAction();
        setContentView(R.layout.diary_edit);
        mTitleText = (EditText) findViewById(R.id.title);
        mBodyText = (EditText) findViewById(R.id.body);
        confirmButton = (Button) findViewById(R.id.confirm);
        button = (Button) findViewById(R.id.delete);

        //插入图片
//        Button takephoto = (Button)findViewById(R.id.take_photo);
//        picture = (ImageView)findViewById(R.id.picture);
//        takephoto.setOnClickListener(new View.OnClickListener(){
//            @Override
//            public void onClick(View v){
//                File outputImage = new File(getExternalCacheDir(),"output_image.jpg");
//                try{
//                    if(outputImage.exists()){
//                        outputImage.delete();
//                    }
//                    outputImage.createNewFile();
//                }catch(IOException e){
//                    e.printStackTrace();
//                }
//                if(Build.VERSION.SDK_INT >=24){
//                    imageUri = FileProvider.getUriForFile(DiaryEditor.this,"com.example.databasediary.fileprovider",outputImage);
//                }else{
//                    imageUri = Uri.fromFile(outputImage);
//                }
//                Intent intent0 = new Intent("android.media.action.IMAGE_CAPTURE");
//                intent0.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
//                startActivityForResult(intent0,TAKE_PHOTO);
//            }
//        });


        if (EDIT_DIARY_ACTION.equals(action)) {// 编辑日记
            mState = STATE_EDIT;
            mUri = intent.getData();
            mCursor = managedQuery(mUri, PROJECTION, null, null, null);
            mCursor.moveToFirst();
            String title = mCursor.getString(1);
            mTitleText.setTextKeepState(title);
            String body = mCursor.getString(2);
            mBodyText.setTextKeepState(body);
            setResult(RESULT_OK, (new Intent()).setAction(mUri.toString()));
            setTitle("编辑日记");
        } else if (INSERT_DIARY_ACTION.equals(action)) {// 新建日记
            mState = STATE_INSERT;
            setTitle("新建日记");
        } else {
            Log.e(TAG, "no such action error");
            finish();
            return;
        }

        confirmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (mState == STATE_INSERT) {
                    insertDiary();
                } else {
                    updateDiary();
                }
                Intent mIntent = new Intent();
                setResult(RESULT_OK, mIntent);
                finish();
            }
        });

        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                getContentResolver().delete(mUri, null, null);
                finish();
            }
        });
    }




    private void insertDiary() {
        String title = mTitleText.getText().toString();
        String body = mBodyText.getText().toString();
        ContentValues values = new ContentValues();
        values.put(Fields.DiaryColumns.CREATED, LifeDiaryContentProvider
                .getFormateCreatedDate());
        values.put(Fields.DiaryColumns.TITLE, title);
        values.put(Fields.DiaryColumns.BODY, body);
        getContentResolver().
                insert(Fields.DiaryColumns.CONTENT_URI, values);

    }

    private void updateDiary() {
        String title = mTitleText.getText().toString();
        String body = mBodyText.getText().toString();
        ContentValues values = new ContentValues();
        values.put(Fields.DiaryColumns.CREATED, LifeDiaryContentProvider
                .getFormateCreatedDate());
        values.put(Fields.DiaryColumns.TITLE, title);
        values.put(Fields.DiaryColumns.BODY, body);
        getContentResolver().
                update(mUri, values,null, null);
    }


    //插入图片
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data){
//        switch (requestCode){
//            case TAKE_PHOTO:
//                if(resultCode == RESULT_OK){
//                    try{
//                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
//                        picture.setImageBitmap(bitmap);
//                    }catch(FileNotFoundException e){
//                        e.printStackTrace();
//                    }
//                }
//                break;
//            default:
//                break;
//        }
//    }

}

